import model.models
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import datastore_errors
from google.appengine.ext.webapp import template

from google.appengine.ext.db import polymodel


class PSModel(db.Model):
	created = db.DateTimeProperty(auto_now_add=True)
	active = db.BooleanProperty(default=True)

class PSCompany(PSModel):
	name = db.StringProperty(required=True)
	responsibles = db.ListProperty(db.Key,verbose_name="Company Responsible")
	
class PSArea(PSModel):
	name = db.StringProperty(required=True)
	company = db.ReferenceProperty(PSCompany,collection_name="areas")
	responsibles = db.ListProperty(db.Key,verbose_name="Company Responsible")

class PSTeam(PSModel):
	name = db.StringProperty(required=True)
	area = db.ReferenceProperty(PSArea,collection_name="teams")
	members = db.ListProperty(db.Key,verbose_name="Team members")

class PSUser(PSModel):
	name = db.StringProperty(required=True)
	email = db.EmailProperty(required=True)
	pword = db.StringProperty(required=True)
	company = db.ReferenceProperty(PSCompany,collection_name="employees")
	area = db.ReferenceProperty(PSArea,collection_name="members")
	projects = db.ListProperty(db.Key,verbose_name="involved_projects")
		
class PSProject(PSModel):
	name = db.StringProperty(required=True)
	company = db.ReferenceProperty(PSCompany,collection_name="projects")
	area = db.ReferenceProperty(PSArea,collection_name="projects")
	members = db.ListProperty(db.Key,verbose_name="project_developers")
	leader = db.ReferenceProperty(PSUser,collection_name="lead_projects")
	default_origin = db.StringProperty()
	default_destination = db.StringProperty()
	
class PSWorkflow(PSModel):
	project = db.ReferenceProperty(PSProject,collection_name="workflows")
	name = db.StringProperty(required=True)

class PSRequest(PSModel):
	requestor = db.ReferenceProperty(PSUser,collection_name="requests")
	workflow = db.ReferenceProperty(PSWorkflow,collection_name="requests")
	name = db.StringProperty()
	origin = db.StringProperty()
	destination = db.StringProperty()
	files_changed = db.StringListProperty()
	required_commands = db.StringListProperty()
	comment = db.TextProperty(required=True)
	status = db.StringProperty(required=True,choices=set(["pending","complete","denied","cancelled","failed"]))
	close_date = db.DateTimeProperty()
	last_updated = db.DateTimeProperty(auto_now_add=True)
	passed_steps = db.ListProperty(db.Key)
	pending_steps = db.ListProperty(db.Key)
	
	def toString(self):
	  result = 		"Request " + self.name + " \nRequested by "+ self.requestor.name
	  result = result + 	"\nOrigin:" + self.origin + "\nDestination:"+self.destination
	  result = result +	"\nFiles Changed: " + str(self.files_changed) 
	  result = result +	"\nRequired Commands: " + str(self.required_commands) 
	  result = result +	"\nComment: " + self.comment + "\n"
	  return result

class PSPolyModel(polymodel.PolyModel):
	date_created = db.DateTimeProperty(auto_now_add=True)
	is_active = db.BooleanProperty(default=True)
	
class PSWorkflowElement(PSPolyModel):
	name = db.StringProperty(required=True)
	description = db.TextProperty()
	workflow = db.ReferenceProperty(PSWorkflow,collection_name="elements")
	priority = db.IntegerProperty(required=True)
	possible_outcomes = db.StringListProperty()
	
class PSWENotifyProjectLeader(PSWorkflowElement):
	pass

class PSWENotifyTeamLeader(PSWorkflowElement):
	pass
      
class PSWENotifyAreaResponsible(PSWorkflowElement):
	area = db.ReferenceProperty(PSArea, collection_name="area_responsible_notifications")

class PSWENotifyCompanyResponsible(PSWorkflowElement):
	pass


class PSWEBroadcastMembers(PSWorkflowElement):
	pass
      
class PSWEApplyOperation(PSWorkflowElement):
	member_to_apply = db.ReferenceProperty(PSUser,collection_name="applied_operations")
	team_to_apply = db.ReferenceProperty(PSTeam,collection_name="applied_operations")
	

class PSStep(db.Expando):
	created = db.DateTimeProperty(auto_now_add=True)
	modified = db.DateTimeProperty()
	modified_by = db.ReferenceProperty(PSUser, collection_name="steps_modified")
	result = db.StringProperty(multiline=True)
	extended_comment = db.TextProperty()
	status = db.StringProperty(default="pending",choices=set(["pending","approved","denied","cancelled","failed"]))
	request = db.ReferenceProperty(PSRequest,collection_name="all_steps")
	workflow_element = db.ReferenceProperty(PSWorkflowElement,collection_name="steps")


